package com.interview.ujiuye3;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Copyright (C), 2018-2020
 * FileName: Demo3
 * Author:   kongfanyu
 * Date:     2020/10/13 14:34
 */
public class Demo3 {

    static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                atomicReference.compareAndSet(100,101);//第一次 AB
                atomicReference.compareAndSet(101,100);//第二次 BA
            }
        }).start();
        System.out.println("第一个线程修改之后的数据:"+atomicReference.get());

        new Thread(new Runnable() {
            @Override
            public void run() {
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                atomicReference.compareAndSet(100,2020);
                System.out.println("第二个线程修改之后的数据:"+atomicReference.get());
            }
        }).start();

    }
}
